package pe.gob.mef.snip.preinversion.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import pe.gob.mef.snip.preinversion.domain.Documento;
import pe.gob.mef.snip.preinversion.domain.Estudio;
import pe.gob.mef.snip.preinversion.domain.ProyectoBanco;
import pe.gob.mef.snip.preinversion.domain.TakeaNumber;
import pe.gob.mef.snip.preinversion.service.DocumentoService;
import pe.gob.mef.snip.preinversion.service.EstudioService;
import pe.gob.mef.snip.preinversion.service.ProyectoBancoService;
import pe.gob.mef.snip.preinversion.service.TakeaNumberService;
import pe.gob.mef.snip.preinversion.util.Parametros;
import pe.gob.mef.snip.preinversion.util.ServiceException;
import pe.gob.mef.snip.preinversion.util.Util;

@Controller
public class EntradaySalidaController {

	protected final Log log = LogFactory.getLog(getClass());
	
	@Autowired
	private DocumentoService documentoService;
	@Autowired
	private ProyectoBancoService proyectoBancoService;
	@Autowired
	private TakeaNumberService takeaNumberService;	
	
	private Documento documento;
	private ProyectoBanco proyectoBanco;
	private TakeaNumber takeaNumber;
	
	@RequestMapping("/entradaySalidaSnip03Listar")
	public ModelAndView entradaySalidaSnip03Listar(HttpServletRequest request, HttpServletResponse response) {
		
		if (log.isDebugEnabled()) log.debug("entradaySalidaSnip03Listar(HttpServletRequest,HttpServletResponse");
	    HttpSession session = request.getSession();	
		Map<String, Object> entradaySalidaSnip03Map = new HashMap<String, Object>();
		
	    String viewName = "";
		try{
			
				entradaySalidaSnip03Map.put("idProyecto", request.getParameter("idProyecto"));
				entradaySalidaSnip03Map.put("version", request.getParameter("version"));
				
				viewName = "frmEntradaySalidas";			
			
			
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView(viewName,"entradaySalidaSnip03Map",entradaySalidaSnip03Map);
		
	}
	
	
	@RequestMapping("/entradaySalidaSnip03Grabar")
	public ModelAndView estudiosSnip03Grabar(HttpServletRequest request, HttpServletResponse response) {
		
		if (log.isDebugEnabled()) log.debug("estudiosSnip03Grabar(HttpServletRequest,HttpServletResponse");
	    HttpSession session = request.getSession();	
		Map<String, Object> entradaySalidaSnip03Map = new HashMap<String, Object>();
		
		documento = new Documento();
		proyectoBanco = new ProyectoBanco();
		takeaNumber = new TakeaNumber();		
	    
		String viewName = "";
	    try{   
				entradaySalidaSnip03Map.put("idProyecto", request.getParameter("idProyecto"));
				entradaySalidaSnip03Map.put("version", request.getParameter("version"));

				takeaNumber.setId((Short)Short.parseShort(Parametros.IDTABLA_DOCUMENTO));
				takeaNumberService.obtenerNextNumber(takeaNumber);	
				
				entradaySalidaSnip03Map.put("idDocumento",takeaNumber.getNextnumber().toString());
				
				documento.setIdDoc(new BigDecimal(takeaNumber.getNextnumber().toString()));
				documento.setProyecto(new BigDecimal(request.getParameter("idProyecto")));
				documento.setDocumento(request.getParameter("txt_nombreDocumento"));
				documento.setFecha(Util.getDateFromString(request.getParameter("txt_fechaDocumento")));
				documentoService.insertar(documento);
				
				viewName = "frmEntradaySalidas";			
		
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView(viewName,"entradaySalidaSnip03Map",entradaySalidaSnip03Map);
	}
	

	
	@RequestMapping("/entradaySalidaSnip04Listar")
	public ModelAndView entradaySalidaSnip04Listar(HttpServletRequest request, HttpServletResponse response) {
		
		if (log.isDebugEnabled()) log.debug("entradaySalidaSnip04Listar(HttpServletRequest,HttpServletResponse");
	    HttpSession session = request.getSession();	
		Map<String, Object> entradaySalidaSnip04Map = new HashMap<String, Object>();
		
	    String viewName = "";
		try{
			
			if(session.getAttribute("proyecto") == null || session.getAttribute("proyecto") == ""){
				viewName = "errorPage";
			}else{
				entradaySalidaSnip04Map.put("proyecto", session.getAttribute("proyecto"));
				entradaySalidaSnip04Map.put("version", session.getAttribute("version"));
				viewName = "frmEntradaySalidas";			
			}
			
			
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//return new ModelAndView(viewName,"entradaySalidaSnip03Map",entradaySalidaSnip03Map);
		return new ModelAndView("frmEntradaySalidas","entradaySalidaSnip04Map",entradaySalidaSnip04Map);
	}
	
	
	@RequestMapping("/entradaySalidaSnip04Grabar")
	public ModelAndView entradaySalidaSnip04Grabar(HttpServletRequest request, HttpServletResponse response) {
		
		if (log.isDebugEnabled()) log.debug("entradaySalidaSnip04Grabar(HttpServletRequest,HttpServletResponse");
	    HttpSession session = request.getSession();	
		Map<String, Object> entradaySalidaSnip04Map = new HashMap<String, Object>();
		
		documento = new Documento();
		proyectoBanco = new ProyectoBanco();
	    
		String viewName = "";
	       
		try{	
			
			if(session.getAttribute("proyecto") == null || session.getAttribute("proyecto") == ""){
				viewName = "errorPage";
			}else{
				entradaySalidaSnip04Map.put("proyecto", session.getAttribute("proyecto"));
				entradaySalidaSnip04Map.put("version", session.getAttribute("version"));
				
				takeaNumber.setId((Short)Short.parseShort(Parametros.IDTABLA_DOCUMENTO));
				takeaNumberService.obtenerNextNumber(takeaNumber);
				
				documento.setIdDoc(new BigDecimal(takeaNumber.getNextnumber()));
				documento.setProyecto(new BigDecimal(request.getParameter("hd_proyecto")));
				documento.setDocumento(request.getParameter("txt_nombreDocumento"));
				documento.setFecha(new Date(request.getParameter("txt_fechaDocumento")));
				documentoService.insertar(documento);
				
				viewName = "frmEntradaySalidas";			
			}
		
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//return new ModelAndView(viewName,"entradaySalidaSnip03Map",entradaySalidaSnip03Map);
		return new ModelAndView("frmEntradaySalidas","entradaySalidaSnip04Map",entradaySalidaSnip04Map);
	}
	
	
	
}

